package com.zb.easypoi_demo.controller;

import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZZB
 * @Description:
 * @Date: create in 2020/8/24 8:32
 */

@RestController
public class ExcelController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    public String demo(){
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("hello","redisCluster");
        String hello = ops.get("hello");
        return hello;
    }

    /**
     * 导出
     *
     * @param response
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response) throws Exception {
        String id="测试";
        Map<String,Object> map=new HashMap<>();
        //数据部分
        map.put("fhdh",1);
        map.put("xhdz","椒江杰克总部");
        List<Map<String,String>> list=new ArrayList<>();

        Map<String,String> xdMap=new HashMap<>();
        xdMap.put("fhdh","fhdh01");
        list.add(xdMap);
        Map<String,String> xdMap2=new HashMap<>();
        xdMap2.put("fhdh","fhdh01");
        list.add(xdMap2);

        map.put("xdList",list);
        XWPFDocument document = WordExportUtil.exportWord07("templates/fhmb.docx", map);
        response.setCharacterEncoding("utf-8");
        id = URLEncoder.encode(id, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8'zh_cn'" + id+".docx");
//            response.setContentType("application/msword");
        ServletOutputStream os = response.getOutputStream();
        document.write(os);
        os.close();

        System.out.println("导出完成");
    }
}
